using ef_core_2025_learn.Entity;
using ef_core_2025_learn.Entity.Configuration;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using Task = ef_core_2025_learn.Entity.Task;

namespace ef_core_2025_learn;

public class MyBoardsContent : DbContext
{
    public DbSet<WorkItem> WorkItems { get; set; }
    public DbSet<Issue> Issues { get; set; }
    public DbSet<Epic> Epics { get; set; }
    public DbSet<Task> Tasks { get; set; }


    public DbSet<User> Users { get; set; }
    public DbSet<Tag> Tags { get; set; }
    public DbSet<Comment> Comments { get; set; }
    public DbSet<Address> Addresses { get; set; }
    public DbSet<WorkItemsState> WorkItemsStates { get; set; }

    public DbSet<PaiSheng>  PaiShengs { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.LogTo(Console.WriteLine, LogLevel.Information).EnableSensitiveDataLogging();
        optionsBuilder.UseMySql(
            "Server=rm-bp1gvpb1n1hd3d23dqo.mysql.rds.aliyuncs.com;Port=3306;Database=MyDb;User=fengliulin;Password=QQwoaini123;",
            new MySqlServerVersion(new Version(8, 0, 21)));
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Fengliulin>().UseTptMappingStrategy();
        // new AddressConfiguration().Configure(modelBuilder.Entity<Address>());

        modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);

        modelBuilder.Entity<Epic>()
            .Property(epic => epic.EndDate)
            .HasPrecision(3);

        modelBuilder.Entity<Task>()
            .Property(task => task.Activity)
            .HasMaxLength(200);

        modelBuilder.Entity<Task>()
            .Property(task => task.RemainingWork)
            .HasPrecision(14, 2);

        modelBuilder.Entity<Issue>()
            .Property(issue => issue.Effort)
            .HasColumnType("decimal(5,2)");

        modelBuilder.Entity<WorkItemsState>()
            .ToTable("work_items_State")
            .Property(state => state.Value)
            .IsRequired()
            .HasMaxLength(60);

        modelBuilder.Entity<WorkItem>(builder =>
        {
            builder.HasOne(workItem => workItem.State)
                .WithMany()
                .HasForeignKey(w => w.StateId);

            builder.Property(x => x.Area).HasColumnType("varchar(200)");
            builder.Property(wi => wi.IterationPath).HasColumnName("iteration_path");

            builder.Property(item => item.Priority).HasDefaultValue(1);
            builder.HasMany(workItem => workItem.Comments)
                .WithOne(comment => comment.WorkItem)
                .HasForeignKey(comment => comment.WorkItemId);
            builder.HasOne(workItem => workItem.Author)
                .WithMany(author => author.WorkItems)
                .HasForeignKey(workItem => workItem.AuthorId);

            builder.HasMany(wori => wori.Tags)
                .WithMany(tag => tag.WorkItems)
                .UsingEntity<WorkItemTag>(workItemTagBuilder => workItemTagBuilder
                        .HasOne(workItemTag => workItemTag.Tag)
                        .WithMany()
                        .HasForeignKey(workItemTag => workItemTag.TagId),
                    workItemTagBuilder => workItemTagBuilder
                        .HasOne(workItemTag => workItemTag.WorkItem)
                        .WithMany()
                        .HasForeignKey(workItemTag => workItemTag.WorkItemId),
                    typeBuilder =>
                    {
                        typeBuilder.HasKey(workItemTag => new { workItemTag.TagId, workItemTag.WorkItemId });
                        typeBuilder.Property(workItemTag => workItemTag.PublishDate)
                            .HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
                    }
                );
        });

        modelBuilder.Entity<Comment>(builder =>
        {
            builder.Property(comment => comment.CreateDate).HasDefaultValueSql("CURRENT_TIMESTAMP(6)");
            builder.Property(comment => comment.UpdateDate).ValueGeneratedOnUpdate();
            builder.HasOne(comment => comment.Author)
                .WithMany(author => author.Comments)
                .HasForeignKey(comment => comment.AuthorId)
                .OnDelete(DeleteBehavior.Restrict);
        });

        modelBuilder.Entity<User>(builder =>
        {
            builder
                .HasOne(user => user.Address)
                .WithOne(address => address.User)
                .HasForeignKey<Address>(address => address.UserId);
        });

        modelBuilder.Entity<WorkItemsState>()
            .HasData(
                new WorkItemsState() { Id = 1, Value = "To Do", },
                new WorkItemsState() { Id = 2, Value = "Doing", },
                new WorkItemsState() { Id = 3, Value = "Done", }
            );

        modelBuilder.Entity<Tag>()
            .HasData(
                new Tag() { Id = 1, Value = "Web" },
                new Tag() { Id = 2, Value = "Ui" },
                new Tag() { Id = 3, Value = "Desktop" },
                new Tag() { Id = 4, Value = "API" },
                new Tag() { Id = 5, Value = "Service" }
            );


    }
}